#!/bin/bash
# File Name   : monitor_task.sh
# Author      : moshan
# Mail        : mo_shan@yeah.net
# Version     : 1.0
# Created Time: 2021-07-22 11:08:00
# Function    : 监控脚本, 检查instance hostinfo两个表的异常
#########################################################################
set -e -o pipefail

work_dir="/opt/soft/cgroup"
conf_dir="${work_dir}/conf"

export PATH="/opt/soft/mysql80/bin:/opt/soft/mysql57/bin:${PATH}"

localhost_ip="$(/sbin/ip a 2>/dev/null|grep inet|grep brd|grep scope|awk '{print $2}'|awk -F'/' '{print $1}'|head -1)" || true
t_alert_task="tb_alert_cgroup_info"

ssh_opt="-o ConnectTimeout=3 -o ConnectionAttempts=5 -o PasswordAuthentication=no -o StrictHostKeyChecking=no"

env="${1}"

. ${conf_dir}/alert.conf

alert_role="${alert["role"]}" #'master','slave','real'
ckey="${alert["key"]}"
tel="${alert["tel"]}"

declare -A alert_task  #保存告警项的字典, 用来判断告警项是否已经恢复, 从表里读出来的告警不在这个字典就说明已经恢复
declare -A insert_sql  #保存告警信息写到表里的sql的字典, 插入告警状态
declare -A update_sql  #保存告警信息写到表里的sql的字典, 更新告警状态
declare -A alert_sql   #告警信息入库

global_alert_count=0
global_next_time=""

function f_send_msg_to_user()
{
    [ "$(date +%H)" -gt 9 -a "$(date +%H)" -le 23 ] && return 0
    declare tel info url
    tel="${1}"
    info="${2}"
    url="http://sms.db.zhuaninc.com/api/sms/send"
    timeout 10s curl --header "Content-Type:application/json" -XPOST ${url} -d "
    {
        \"group\":\"DBA\",
        \"mo\":\"${tel}\",
        \"ms\":\"${info}\"
    }"

    return 0
}

function f_send_alert_to_user()
{
    app_code="ed077edb4e761e71c1f7f3b9faa951ef"
    user="${1}"
    info="告警时间 : $(date "+%F %T")\n$(tr "," "\n" <<< ${2})\n\n\n请及时联系DBA进行处理"
    url="wxmsg.zhuaninc.com/api/message/send"
    link_url="t21001.tdb.zhuaninc.com"
    curl --header "Content-Type:application/json" ${url} -d "
        {
            \"app_code\":\"${app_code}\",
            \"to_user\":\"${user}\",
            \"msg\":\"${info}\",
            \"msg_type\":\"0\",
            \"link_url\":\"${link_url}\"
        }"
}

function f_send_msg_to_bot()
{
    info="${1}\n\n\n 告警来源 : ${localhost_ip} : $(realpath "$0")"
    dbkey="${2}"
    no_msg="${3}"
    timeout 10s curl "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${dbkey}" \
    -H 'Content-Type: application/json' \
    -d "
    {
         \"msgtype\": \"text\",
         \"text\": {
             \"content\": \"告警时间 : $(date '+%F %T')\n$(tr "," "\n" <<< ${info})\"
         }
    }"

    if [ "${no_msg}x" == "nox" ]
    then
        return 0
    fi

    f_send_msg_to_user "${tel}" "${info}"
}

function f_write_to_table()
{
    #将告警写到表里面
    declare _info _remarks _sql his_alert_count _count igid alert_value sql
    _info="${1}"
    _remarks="${2}"
    _write="${3}"
    _count="${4}"
    igid="${5}"
    alert_value="${6}"
    his_alert_count="select ifnull(max(alert_count),0) + 1 from ${t_alert_task} 
       where alert_task = '${_info}' and alert_state = 1 and u_time > DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 0 DAY), '%Y-%m-%d 00:00:00') limit 1"
    his_alert_count="$(timeout 10s ${tidb_comm} -NBe "${his_alert_count}" 2>/dev/null)"
    [ -n "${_count}" ] && his_alert_count="${_count}"
    global_alert_count=${his_alert_count}
    [ "${his_alert_count}" -gt 10 ] && return 0 #每天只告警10次

    alert_list="select count(*) from ${t_alert_task} 
        where alert_state = 1 and u_time > now() and alert_task = '${_info}';"
    alert_list="$(timeout 10s ${tidb_comm} -NBe "${alert_list}" 2>/dev/null)"

    if [ "${alert_list}" -gt 0 ]
    then
        sql="update ${t_alert_task} set a_time = now() ,alert_remarks = '${_remarks}'
           where alert_state = 1 and u_time > now() and alert_task = '${_info}';"
        echo "${sql}" |timeout 60s ${tidb_comm} 2>/dev/null
        return 0
    fi

    [ "${_write}x" == "yesx" ] && return 0

    global_next_time="$(date -d "+$((${his_alert_count}*5+1)) min" "+%F %T")"

    sql="replace into ${t_alert_task}(igid,alert_task,alert_state,alert_count,a_time,u_time,alert_remarks,alert_value)
        select ${igid},'${_info}',1, ${his_alert_count},now(), now(),'${_remarks}','${alert_value}';"

    echo "${sql}" |timeout 60s ${tidb_comm} 2>/dev/null

    return 0
}

function f_check_alert_task()
{
    #这是检查告警脚本的任务 /opt/dbcloud/tidb/py3env/bin/python /opt/dbcloud/tidb/monitor/AlertManager.py
    declare _sql _is_ok
    _sql="select count(*) from ${t_alert_task} where alert_state = 0 and u_time > date_add(now(), INTERVAL - 5 MINUTE)"
    _is_ok="$(timeout 10s ${tidb_comm} -NBe "${_sql}" 2>/dev/null)"

    if [ "${_is_ok}x" == "0x" ]
    then
        alert_info="告警原因 : cgroup告警脚本工作状态异常(${1})"
        f_send_msg_to_bot "${alert_info}" "${ckey}" "no"
    fi

    return 0
}

function f_check_alert_ok()
{
    #检查都有哪些告警已经恢复
    declare _sql alert_task_list _task task remarks
    declare -A alert_ok_list
    _sql="select concat(alert_task,':::',alert_remarks) from ${t_alert_task}
        where alert_state = 1 and a_time < date_add(now(), INTERVAL - 5 MINUTE);"
    echo "${_sql}"
    alert_task_list=($(timeout 10s ${tidb_comm} -NBe "${_sql}" 2>/dev/null))
    for _task in ${alert_task_list[@]}
    do
        task="$(awk -F::: '{print $1}' <<< "${_task}")"
        remarks="$(awk -F::: '{print $2}' <<< "${_task}")"

        [ -z "${remarks}" ] && continue
        echo "${alert_task["${task}"]}x"
        if [ "${alert_task["${task}"]}x" != "truex" ]
        then
            update_sql["${task}"]="update ${t_alert_task} set alert_state = 0,alert_count = 0 ,u_time = '$(date '+%F %T')' where alert_task = '${task}' and alert_state = 1;"

            if [ -z "${alert_ok_list["${remarks}"]}" ]
            then
                alert_ok_list["${remarks}"]="${task}"
            else
                alert_ok_list["${remarks}"]="${alert_ok_list["${remarks}"]},${task}"
            fi

            if [ "${remarks}x" == "资源组触发告警x" ]
            then
                declare -A user_list
                declare host port opt sql res
                for j in $(echo ${task}|tr "," " ")
                do
                    host="$(awk -F: '{print $1}' <<< "${j}")"        
                    port="$(awk -F: '{print $2}' <<< "${j}")"        
                    opt="$(awk -F: '{print $3}' <<< "${j}")"        
                    sql="select concat(alert_type,':',alert_channel) from tb_cgroup_manager where istate = 1 and rule = 'add' and rshost = '${host}' and rsport = ${port} and isotype = '${opt}';"
                    res="$(${mysql_comm} -NBe "${sql}" 2>/dev/null)"
                    if [ -z "${res}" ]
                    then
                        res="group:${ckey}"
                    fi

                    if [ -z "${user_list["${res}"]}" ]
                    then
                        user_list["${res}"]="${j}"
                    else
                        user_list["${res}"]="${j},${user_list["${res}"]}"
                    fi
                done
            else
                host=""
                port=""
                opt=""
            fi
        fi
    done

    echo "begin;${update_sql[@]}commit;" |timeout 60s ${tidb_comm} 2>/dev/null

    declare -A alert_info

    db="cgroup"

    if [ "${#alert_ok_list[@]}x" != "0x" ]
    then
        for i in ${!alert_ok_list[@]}
        do

            if [ -z "${alert_info[${db}]}" ]
            then
                alert_info["${db}"]="${i}:${alert_ok_list["${i}"]}"
            else
                alert_info["${db}"]="${alert_info["${db}"]}\n${i}:${alert_ok_list["${i}"]}"
            fi
        done

        for db in ${!alert_info[@]}
        do
            f_send_msg_to_bot "告警恢复\n        恢复列表 : ${alert_info["${db}"]}" "${ckey}" "no"
        done

        declare alert_type alert_channel

        for k in ${!user_list[@]}
        do
            alert_type="$(awk -F: '{print $1}' <<< "${k}")"
            alert_channel="$(awk -F: '{print $2}' <<< "${k}")"
            if [ "${alert_type}x" == "userx" ]
            then
                f_send_alert_to_user "${alert_channel}" "告警恢复\n        恢复列表 : ${user_list["${k}"]}" || f_send_msg_to_bot "给${alert_channel}发送恢复信息失败" "${ckey}" || return 1
            else
                f_send_msg_to_bot "告警恢复\n        恢复列表 : ${user_list["${k}"]}" "${alert_channel}" "no" || f_send_msg_to_bot "给${alert_channel}发送恢复信息失败" "${ckey}" "no" || return 1
            fi
        done
    fi
}

function f_check_rule()
{
    declare sql i alert_type alert_channel alert_info alert_id update_sql
    declare -a list
    sql="select concat(alert_type,'@@@',alert_channel,'@@@',group_concat(concat(dbtype,':',rshost,':',rsport,':',rule,':',isotype)),'@@@',group_concat(id)) from tb_cgroup_manager where istate = -1 group by alert_type,alert_channel;"
    list=($(${mysql_comm} -NBe "${sql}" 2>/dev/null))
    key_list=""
    for i in ${list[@]}
    do
        alert_type="$(awk -F@@@ '{print $1}' <<< "${i}")"
        alert_channel="$(awk -F@@@ '{print $2}' <<< "${i}")"
        alert_info="告警内容 : cgroup资源组配置异常，异常列表如下\n$(awk -F@@@ '{print $3}' <<< "${i}")"
        [ -z "${err}" ] && err="${alert_info}" || err="${err},${alert_info}"
        alert_id="$(awk -F@@@ '{print $4}' <<< "${i}")"
        update_sql="update tb_cgroup_manager set istate = -2 where id in (${alert_id})"
        if [ "${alert_type}x" == "userx" ]
        then
            if f_send_alert_to_user "${alert_channel}" "${alert_info}"
            then
                true
            else
                f_send_msg_to_bot "给${alert_channel}发送告警信息失败" "${ckey}" || continue
            fi
        else
            key_list="${key_list},${alert_channel}"
            if f_send_msg_to_bot "${alert_info}" "${alert_channel}"
            then
                true
            else
                f_send_msg_to_bot "给${alert_channel}发送告警信息失败" "${ckey}" || continue
            fi
        fi

        ${mysql_comm} -NBe "${update_sql}"

    done
    
    if [ -n "${err}" ]
    then
        if ! echo "${key_list}" | grep ${ckey}
        then
            f_send_msg_to_bot "${alert_info}" "${ckey}" || continue
        fi
    fi

    update_sql="update tb_cgroup_manager set istate = 3 ,rule = 'del' where e_time < now();"
    ${mysql_comm} -NBe "${update_sql}"

    return 0
}

function f_check_monitor()
{
    declare sql i alert_task_tmp

    sql="select concat(b.rshost,':',b.rsport,':',isotype,':',a.rsrole,':',SUBSTRING_INDEX(GROUP_CONCAT(CONCAT(mvalue, '(当前值):', isovalue,'(告警值)') ORDER BY mvalue DESC),',', 1)) 
         from tb_mysql_role a join
             (
                 select rshost,rsport,isotype,round(sum(mvalue)/count(*),0) mvalue,round(sum(isovalue)/count(*),0) isovalue 
                 from tb_monitor_cgroup_info_his
                 where a_time >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) 
                 group by rshost,rsport,isotype
             ) b on a.rshost = b.rshost and a.rsport = b.rsport 
        where a.rsrole in (${alert_role}) and (
               (isotype = 'cpu' and mvalue > isovalue * ${value['cpu']}) 
            or (isotype like 'io%' and mvalue > isovalue * ${value['io']}) 
            or (isotype = 'memory' and mvalue > isovalue * ${value['memory']})
        ) group by b.rshost,b.rsport,a.rsrole,b.isotype;"
    echo "${sql}"
    list=($(${tidb_comm} -NBe "${sql}" 2>/dev/null))
    for i in ${list[@]}
    do
        alert_task_tmp="$(awk -F: '{print $1":"$2":"$3":"$4}' <<< "${i}")"
        alert_task["${alert_task_tmp}"]="true"
        f_write_to_table "${alert_task_tmp}" "资源组触发告警" "" "" "$(awk -F: '{print $2}' <<< "${i}")" "${i}"
    done
}

function f_check_monitor_write_err()
{
    #监控数据上报异常
    declare sql i
    declare file1 file2
    file1="$(pwd)/log/res.${env}.log"
    file2="$(pwd)/log/res2.${env}.log"
    mkdir -p $(pwd)/log
    sql="select concat(rshost,':',rsport,':',isotype) from tb_cgroup_manager where istate = 1 and rule = 'add' group by rshost,rsport,isotype;"
    ${mysql_comm} -NBe "${sql}" 2>/dev/null | sort -u > ${file1}
    sql="select concat(rshost,':',rsport,':',isotype) from tb_monitor_cgroup_info_his where a_time >= DATE_SUB(NOW(), INTERVAL 2 MINUTE) and rsport != 0 and isotype not in ('load','iowait','usagemem','susagemem') group by rshost,rsport,isotype;"
    ${tidb_comm} -NBe "${sql}" 2>/dev/null|sort -u > ${file2}
    comm -23 ${file1} ${file2}|while read i
    do
        t=$(date +%s)
        cp ${file2} ${file2}.${t}
        cp ${file1} ${file1}.${t}
        f_write_to_table "${i}" "监控上报异常" "" "" "$(awk -F: '{print $2}' <<< "${i}")" "${i}"
    done

    comm -13 ${file1} ${file2}|while read i
    do
        f_write_to_table "${i}" "规则可能已经删除" "" "" "$(awk -F: '{print $2}' <<< "${i}")" "${i}"
    done

    sql="select concat(rshost,':',rsport,':',group_concat(isotype SEPARATOR '|')) from tb_cgroup_manager group by rshost,rsport having count(*) < 6;"
    ${mysql_comm} -NBe "${sql}" 2>/dev/null |while read line
    do
        f_write_to_table "${line}" "资源组少于6组" "" "" "$(awk -F: '{print $2}' <<< "${line}")" "${line}"
    done

    return 0
}

function f_check_upgrade()
{
    declare sql rshost rsport isotype value mvalue check_ok exe_state tmp_opt
    rshost="${1}"
    rsport="${2}"
    isotype="${3}"

    for loop in {1..20}
    do
        sql="select isovalue from tb_monitor_cgroup_info where a_time >= DATE_SUB(NOW(), INTERVAL 2 MINUTE) and rshost = '${rshost}' and rsport = ${rsport} and isotype = '${isotype}' limit 1;"

        value="$(${tidb_comm} -NBe "${sql}" 2>/dev/null)" && exe_state="${?}" || exe_state="${?}" #旧的配置值

        [ "${exe_state}x" != "0x" ] && break

        [ -z "${value}" ] && sleep 0.1
    done

    if [ "${exe_state}x" != "0x" ]
    then
        f_send_msg_to_bot "${rshost}:${rsport}:${isotype}自动升级配置失败 : 获取旧配置异常" "${ckey}"
        return 0
    fi

    if [[ "${value}" =~ ^-?[0-9]+$ ]]
    then
        true
    else
        f_send_msg_to_bot "${rshost}:${rsport}:${isotype}自动升级配置失败 : 旧配置的值不合法[${value}]" "${ckey}"
        return 0
    fi

    sql="select rshost from tb_upgrade_cgroup_info where a_time >= DATE_SUB(NOW(), INTERVAL 120 MINUTE) and rshost = '${rshost}' and rsport = ${rsport} and isotype = '${isotype}';"

    check_ok="$(${tidb_comm} -NBe "${sql}" 2>/dev/null)" && exe_state="${?}" || exe_state="${?}"

    if [ "${exe_state}x" != "0x" ]
    then
        f_send_msg_to_bot "${rshost}:${rsport}:${isotype}自动升级配置失败 : 查看最近两小时是否已经升级配置异常" "${ckey}"
        return 0
    elif [ "${rshost}x" == "${check_ok}x" ]
    then
        f_send_msg_to_bot "${rshost}:${rsport}:${isotype}自动升级配置失败 : 最近两小时已经存在升级配置任务" "${ckey}"
        return 0
    fi

    if [ "${isotype}x" == "iops_readx" ] || [ "${isotype}x" == "iops_writex" ]
    then #iops相关要求是100的整数倍
        mvalue="$(awk '{if(($1*1.3)%100 > 0) {print (int($1*1.3/100)+1)*100} else print(int($1*1.3))}' <<< "${value}")"
        tmp_opt="iops"
    elif [ "${isotype}x" == "io_readx" ] || [ "${isotype}x" == "io_writex" ]
    then #io流量相关要求是10的整数倍
        mvalue="$(awk '{if(($1*1.3)%10 > 0) {print (int($1*1.3/10)+1)*10} else print(int($1*1.3))}' <<< "${value}")"
        tmp_opt="io"
    elif [ "${isotype}x" == "cpux" ]
    then #cpu相关要求是1的整数倍，要求最少是2c
        mvalue="$(awk '{if($1*1.3 > int($1*1.3)) {res = int($1*1.3)+1} else res = int($1*1.3)}END{print (res - $1 > 2 ? res : $1 + 2)}' <<< "${value}")"
        tmp_opt="cpu"
    elif [ "${isotype}x" == "mem_oomx" ] || [ "${isotype}x" == "memx" ] || [ "${isotype}x" == "memoryx" ] || [ "${isotype}x" == "memory_oomx" ]
    then #memory相关要求是1的整数倍，且要求最少8GB
        mvalue="$(awk '{if($1*1.3 > int($1*1.3)) {res = int($1*1.3)+1} else res = int($1*1.3)}END{print (res - $1 > 8 ? res : $1 + 8)}' <<< "${value}")"
        tmp_opt="memory"
    fi

    if [[ "${mvalue}" =~ ^-?[0-9]+$ ]]
    then
        true
    else
        f_send_msg_to_bot "${rshost}:${rsport}:${isotype}自动升级配置失败 : 新配置的值不合法" "${ckey}"
        return 0
    fi

    if [ ${mvalue} -ge ${max_value["${tmp_opt}"]} ]
    then
        if [ ${value} -ge ${max_value["${tmp_opt}"]} ]
        then
            f_send_msg_to_bot "${rshost}:${rsport}:${isotype}自动升级配置失败 : 当前配置(${isotype}:${value})已经是允许的最大配置，请dba干预处理。" "${ckey}"
            return 0
        else
            mvalue="${max_value["${tmp_opt}"]}"
        fi
    fi

    f_send_msg_to_bot "${rshost}:${rsport}:${isotype}自动升级配置 : ${value} --> ${mvalue}" "${ckey}" "no"

    sql="insert into tb_upgrade_cgroup_info(rshost,rsport,isotype,isovalue,a_time,remarks) value('${rshost}',${rsport},'${isotype}','${value} --> ${mvalue}',now(),'自动升配');"

    echo "${sql}"|timeout 60s ${tidb_comm} 2>/dev/null

    sql="update tb_cgroup_manager set istate = 3, isovalue = ${mvalue} where rshost = '${rshost}' and rsport = '${rsport}' and isotype = '${isotype}'"

    ${mysql_comm} -NBe "${sql}"

    return 0
}

function f_send_alert()
{
    declare sql i alert_info remarks value host port opt j k
    declare list
    sql="select concat(alert_remarks,':::', group_concat(alert_value)) from tb_alert_cgroup_info where alert_state = 1 and u_time <= now() and alert_count < 10 group by alert_remarks;"
    list=($(${tidb_comm} -NBe "${sql}" 2>/dev/null))
    for i in ${list[@]}
    do
        remarks="$(awk -F::: '{print $1}' <<< "${i}")"
        value="$(awk -F::: '{print $2}' <<< "${i}")"
        if [ "${remarks}x" == "资源组触发告警x" ]
        then
            declare -A user_list
            for j in $(echo ${value}|tr "," " ")
            do
                host="$(awk -F: '{print $1}' <<< "${j}")"        
                port="$(awk -F: '{print $2}' <<< "${j}")"        
                opt="$(awk -F: '{print $3}' <<< "${j}")"        
                f_check_upgrade "${host}" "${port}" "${opt}" || true
                sql="select concat(alert_type,':',alert_channel) from tb_cgroup_manager where istate = 1 and rule = 'add' and rshost = '${host}' and rsport = ${port} and isotype = '${opt}';"
                res="$(${mysql_comm} -NBe "${sql}" 2>/dev/null)"
                if [ -z "${user_list["${res}"]}" ]
                then
                    user_list["${res}"]="${j}"
                else
                    user_list["${res}"]="${j},${user_list["${res}"]}"
                fi
            done
        else
            host=""
            port=""
            opt=""
        fi

        if [ -z "${alert_info}" ]
        then
            alert_info="告警内容 : ${remarks}，异常列表如下\n${value}"
        else
            alert_info="${alert_info}\n\n\n告警内容 : ${remarks}，异常列表如下\n${value}"
        fi
    done

    declare alert_type alert_channel

    for k in ${!user_list[@]}
    do
        alert_type="$(awk -F: '{print $1}' <<< "${k}")"
        alert_channel="$(awk -F: '{print $2}' <<< "${k}")"
        if [ "${alert_type}x" == "userx" ]
        then
            f_send_alert_to_user "${alert_channel}" "${user_list["${k}"]}" || f_send_msg_to_bot "给${alert_channel}发送告警信息失败" "${ckey}" || return 1
        else
            f_send_msg_to_bot "${user_list["${k}"]}" "${alert_channel}" || f_send_msg_to_bot "给${alert_channel}发送告警信息失败" "${ckey}" || return 1
        fi
    done

    if [ -n "${alert_info}" ]
    then

        f_send_msg_to_bot "${alert_info}" "${ckey}" || return 1

        echo "update tb_alert_cgroup_info set u_time = date_add(now(), INTERVAL + (alert_count * 5 + 1) MINUTE) where alert_state = 1 and u_time <= now();" |timeout 60s ${tidb_comm} 2>/dev/null
    fi

    echo "replace into tb_alert_cgroup_info(igid,alert_task,alert_state,alert_count,a_time,u_time,alert_remarks) value(0,'cgroup_alert_check',0,0,now(),now(),'');" |timeout 60s ${tidb_comm} 2>/dev/null

    return 0

}

function f_sync_4097_metadata_to_tidb()
{

    declare sql res exe_state tab
    exe_state="0"
    sql="select count(*) from tb_mysql_role where a_time >= DATE_SUB(NOW(), INTERVAL 120 MINUTE)"
    res="$(${tidb_comm} -NBe "${sql}")" || exe_state="${?}"
    if [ "${env}x" == "devx" ]
    then
        tab="tb_test_mysql_instance"
    else
        tab="tb_mysql_instance"
    fi

    if [ "${exe_state}x" == "0x" ] && [ "${res}x" == "0x" ]
    then #两小时同步一次
        ${tidb_comm} -NBe "truncate table tb_mysql_role;"
        sql="select concat('replace into tb_mysql_role(rshost,rsport,rsrole,a_time) select \"',rshost,'\",','\"',rsport,'\",\"',rsrole,'\",now();') from ${tab} where istate = '0';"
        ${mysql_comm} -NBe "${sql}"|${tidb_comm}
    fi
}

function f_main()
{
    if [ "${env}x" != "devx" ] && [ "${env}x" != "oltpx" ]
    then
        return 1
    else
        source ${conf_dir}/${env}.conf
    fi

    mysql_comm="${mysql_path} -u${mysql_user} -p${mysql_pass} -h${mysql_host} -P ${mysql_port} ${mysql_db}"
    tidb_comm="${mysql_path} -u${tidb_user} -p${tidb_pass} -h${tidb_host} -P ${tidb_port} ${tidb_db}"
    is_master="$(timeout 10s ${mysql_comm} -NBe "show slave status\G" 2>/dev/null|wc -l)"
    if [ "${is_master}x" != "0x" ]
    then
        exit
    fi

    f_sync_4097_metadata_to_tidb &
    f_check_alert_task "${env}"
    f_check_rule
    f_check_monitor
    f_check_alert_ok
    f_check_monitor_write_err
    f_send_alert
}

f_main
